import 'package:flutter/material.dart';
import 'package:flutter_default_project/models.dart';
import 'package:flutter_default_project/state_container.dart';
import 'package:flutter_default_project/widgets/stat.dart';
import 'package:flutter_default_project/widgets/todo_list.dart';
import 'package:todos_app_core/todos_app_core.dart';

class HomeScreen extends StatefulWidget {
  const HomeScreen({super.key});

  @override
  State<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  bool isPageHome = true;
  @override
  Widget build(BuildContext context) {
    final stateContainer = StateContainer.of(context);
    return Scaffold(
      appBar: AppBar(
        title: const Text('Home Screen'),
        actions: [
          PopupMenuButton(
            onSelected: (selected) {
              switch (selected) {
                case ExtraAction.clearCompleted:
                  stateContainer.clearCompleted();
                  break;
                case ExtraAction.toggleAll:
                  stateContainer.toggleAll();
                  break;
              }
            },
            itemBuilder: (context) {
              return [
                PopupMenuItem(
                  value: ExtraAction.clearCompleted,
                  child: Text('clear complete'),
                ),
                PopupMenuItem(
                  value: ExtraAction.toggleAll,
                  child: Text('toggle all'),
                ),
              ];
            },
          ),
          PopupMenuButton(
            onSelected: (selected) {
              stateContainer.updateVisibilityFilter(selected);
            },
            itemBuilder: (context) {
              return [
                PopupMenuItem(
                  value: VisibilityFilter.all,
                  child: Text('all todos'),
                ),
                PopupMenuItem(
                  value: VisibilityFilter.active,
                  child: Text('active todos'),
                ),
                PopupMenuItem(
                  value: VisibilityFilter.completed,
                  child: Text('completed todos'),
                ),
              ];
            },
            child: Icon(Icons.more_rounded),
          ),
        ],
      ),
      body: isPageHome ? TodoList() : Stat(),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          Navigator.of(context).pushNamed(ArchSampleRoutes.addTodo);
        },
        child: Icon(Icons.add),
      ),
      bottomNavigationBar: BottomNavigationBar(
        onTap: (index) {
          setState(() {
            isPageHome = index == 0;
          });
        },
        items: [
          BottomNavigationBarItem(icon: Icon(Icons.list), label: 'home'),
          BottomNavigationBarItem(
            icon: Icon(Icons.line_axis_rounded),
            label: 'stat',
          ),
        ],
      ),
    );
  }
}
